<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="代理" prop="agentName">
                <el-input v-model="queryParams.agentName" placeholder="请输入代理" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['common:config:add']">新增</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="id" align="center" prop="id" />
            <el-table-column label="代理" align="center" prop="agentName" />
            <el-table-column label="渠道" align="center" prop="channel" >
              <template #default="scope">
                <dict-tag :options="dict.type.sys_channel" :value="scope.row['channel']"/>
              </template>
            </el-table-column>
            <el-table-column label="商店返点" align="center" prop="storePoint" />
            <el-table-column label="非商店返点" align="center" prop="notStorePoint" />
            <el-table-column label="信息流返点" align="center" prop="informationFlowPoint" />
            <el-table-column label="搜索返点" align="center" prop="searchPoint" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['common:config:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:post:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改账户对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
                <el-form-item label="代理名称" prop="agentCode">
                    <el-select v-model="form.agentCode" placeholder="代理"  @change="_agentChange" filterable style="width:100%">
                        <el-option
                        v-for="item in dict.type.ACCOUNT_AGENT"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                        </el-option>
                    </el-select>
                    <!-- <el-input v-model="form.agentName" placeholder="请输入代理名称" hidden /> -->
                </el-form-item>
                <el-form-item label="渠道" prop="channel">
                    <el-select v-model="form.channel" placeholder="渠道" clearable style="width:100%">
                        <el-option
                            v-for="item in dict.type.sys_channel"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                    </el-select>

                </el-form-item>
                <el-form-item label="商店返点" prop="storePoint">
                    <el-input v-model="form.storePoint" placeholder="请输入商店返点" />
                </el-form-item>
                <el-form-item label="非商店返点" prop="notStorePoint">
                    <el-input v-model="form.notStorePoint" placeholder="请输入非商店返点" />
                </el-form-item>
                <el-form-item label="信息流返点" prop="informationFlowPoint">
                    <el-input v-model="form.informationFlowPoint" placeholder="请输入信息流返点" />
                </el-form-item>
                <el-form-item label="搜索返点" prop="searchPoint">
                    <el-input v-model="form.searchPoint" placeholder="请输入搜索返点" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listTable, getDetail, addItem, updateItem, delItem } from "@/api/place-manage/agent";

export default {
    name: "PlaceAgent",
    dicts: ['sys_channel', 'ACCOUNT_AGENT'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 账户表格数据
            postList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                agentName: ''
            },
            // 表单参数
            form: {
                agentName: null,
                channel: null,
                storePoint: 0,
                notStorePoint: 0,
                informationFlowPoint: 0,
                searchPoint: 0,
            },
            // 表单校验
            rules: {
                agentCode: [
                    { required: true, message: "代理商名称不能为空", trigger: "blur" }
                ],
                channel: [
                    { required: true, message: "渠道不能为空", trigger: "change" }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        _agentChange(n, o) {
            let selectItem = this.dict.type.ACCOUNT_AGENT.filter(item => item.value == n)
            this.form.agentName = selectItem[0].label
        },
        /** 查询账户列表 */
        getList() {
            this.loading = true;
            listTable(this.queryParams).then(response => {
                this.postList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                agentName: null,
                channel: null,
                storePoint: 0,
                notStorePoint: 0,
                informationFlowPoint: 0,
                searchPoint: 0,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.postId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "新增";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const rowId = row.id || this.ids
            getDetail(rowId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateItem(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addItem(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const postIds = row.id || this.ids;
            this.$modal.confirm('是否确认删除代理编号为"' + postIds + '"的数据项？').then(function () {
                return delItem(postIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        }
    }
};
</script>
